package org.example.zuoye.api;

import org.example.zuoye.entity.Patient;
import org.example.zuoye.entity.Result;
import org.example.zuoye.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author xinchen
 * @Date: 2024/08/13/ 11:43
 * @Description
 */
@RestController
@RequestMapping(value = "/api/v1/patient")
public class PatientApi {

    @Autowired
    private PatientService patientService;

    /**
     * select
     */
    @GetMapping
    public Result search(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,
                         Patient patient) {
        return Result.success(patientService.search(page, limit, patient));
    }

    /**
     * remove
     */
    @DeleteMapping
    public Result remove(@RequestBody Integer[] ids) {
        try {
            patientService.remove(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * add
     */
    @PostMapping
    public Result add(@RequestBody Patient patient) {
        try {
            patientService.add(patient);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * edit
     */
    @PutMapping
    public Result edit(@RequestBody Patient Patient) {
        try {
            patientService.edit(Patient);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 充值操作
     */
    @PutMapping("/money")
    public Result editByMoney(@RequestBody Patient patient) {
        try {
            patientService.editByMoney(patient);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}



